<?php

class WidgetFormUnidad extends sfWidgetForm
{

	public function configure($options = array(), $attributes = array())
	{
		$url = sfContext::getInstance()->getController()->genUrl('unidad/buscar');

		$this->addOption('template',
<<<EOF
<input type="text" id="{input.id}" value="{input.value}" class="buscar_form txt_buscar">
<a class="clear" href="" style="position: relative; top: 4px; left: -25px; text-decoration: none;" title="Reiniciar">
  <span class="clear_bg_loading"></span>
  <span class="clear_bg"></span>
</a>

EOF
		);
	}

	public function getJavascripts()
	{
		return array('/js/unidad.js');
	}

	public function render($name, $value = null, $attributes = array(), $errors = array())
	{
		$template_vars = array(
			'{target.id}' => 'target',
			'{input.id}' => 'txt_buscar_unidad',
			'{input.value}' => '',
		);

		$list = array();
		
		if (!empty($value))
			$list = Doctrine_Core::getTable('Unidad')->createQuery()->whereIn('id', $value)->execute();

		$widget_data = $this->getOption('template');
		$attributes['id'] = $template_vars['{target.id}'];
		$attributes['name'] = $name;

		$attributes['type'] = 'hidden';
		$attributes['value'] = $value;
		$template_vars['{input.value}'] = isset($list[0]) ? $list[0]->__toString() : '';

		$widget_data = $widget_data . $this->renderTag('input', $attributes);

		return strtr($widget_data, $template_vars);
	}
	

}
